import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hygf/view/dialog/update_dialog.dart';
import '../../res/HDColors.dart';
import '../../res/my_colors.dart';
import 'action_sheet.dart';

class DialogManager {
  static void showUpdateDialog(BuildContext context,
      {required String url,
      String title = "发现新版本",
      String content = "发现新版本，是否更新",
      required bool force,
      required bool autoUpload,
      String version = "1.0.0",
      Function()? cancelListener}) {
    showModalBottomSheet(
        context: context,
        enableDrag: false,
        isDismissible: !force,
        backgroundColor: Colors.transparent,
        // shape: RoundedRectangleBorder(borderRadius: BorderRadius.only(topLeft: Radius.circular(16), topRight: Radius.circular(16))),
        builder: (ctx) {
          return UpdateDialog(
            parentContext: context,
            title: title,
            content: content,
            url: url,
            force: force,
            version:version,
            autoUpload: autoUpload,
          );
        });
  }

  static Future<dynamic> showActionSheet(
      /*BuildContext context*/
      {String title = "",
      required List<dynamic> items,
      String displayKey = 'name'}) async {
    // showModalBottomSheet(
    //     context: context,
    //     builder: (context) {
    //       return ActionSheet();
    //     });

    return await Get.bottomSheet(
        ActionSheet(
          title: title,
          items: items,
          displayKey: displayKey,
        ),
        isScrollControlled: true);
  }

  static showDateTimePicker(BuildContext context,
      {

      /// 能滚动到的最小日期
      DateTime? minDateTime,

      /// 能滚动到的最大日期
      DateTime? maxDateTime,

      /// 初始选择的时间。默认当前时间
      DateTime? initialDateTime,

      /// 时间格式化的格式
      String dateFormat = "yyyy年,MM月,dd日,HH时,mm分",

      /// 时间选择组件显示的时间类型
      DateTimePickerMode mode = DateTimePickerMode.datetime,

      /// 点击确认时的回调
      Function(DateTime dateTime)? onConfirm}) {
    BrnDateTimePickerMode brnMode = BrnDateTimePickerMode.datetime;
    switch (mode) {
      case DateTimePickerMode.date:
        brnMode = BrnDateTimePickerMode.date;
        break;
      case DateTimePickerMode.time:
        brnMode = BrnDateTimePickerMode.time;
        break;
      case DateTimePickerMode.datetime:
        brnMode = BrnDateTimePickerMode.datetime;
        break;
      default:
    }

    BrnDatePicker.showDatePicker(context,
        minDateTime: minDateTime,
        maxDateTime: maxDateTime,
        initialDateTime: initialDateTime,
        dateFormat: dateFormat,
        pickerMode: brnMode, onConfirm: (dateTime, list) {
      if (onConfirm != null) {
        onConfirm(dateTime);
      }
    });
  }

  static showAlertDialog(BuildContext context,
      {String? title,
      String? content,
      List<String>? btns,
      Function(int)? onClick}) {
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: title == null ? null : Text(title),
            content: content == null ? null : Text(content),
            actions: _getAlertActions(context, btns, onClick),
          );
        });
  }

  static List<Widget> _getAlertActions(
      BuildContext context, List<String>? btns, Function(int)? onClick) {
    List<Widget> widgets = [];
    if (btns == null) {
      return [];
    }
    btns.asMap().forEach((key, value) {
      var btn = TextButton(
          onPressed: () {
            Navigator.pop(context);
            if (onClick != null) {
              onClick(key);
            }
          },
          child: Text(
            value,
            style: TextStyle(color: MyColors.accent),
          ));
      widgets.add(btn);
    });
    return widgets;
  }

  /// 提示弹框
  static void showTipDialog(
      {textContent = "",
      textCancel = "取消",
      textConfirm = "确定",
      VoidCallback? onCancel,
      VoidCallback? onConfirm,
      barrierDismissible = false,
      WillPopCallback? onWillPop}) {
    Get.defaultDialog(
        radius: 5,
        title: '',
        titlePadding: const EdgeInsets.all(0),
        //是否可以通过点击背景关闭弹窗
        barrierDismissible: barrierDismissible,
        // 拦截关闭之前做一些操作
        onWillPop: onWillPop,
        // titleStyle: TextStyle(fontSize: 16, color: HDColors.black),
        content: Center(
            child: Container(
                margin: const EdgeInsets.fromLTRB(15, 10, 15, 10),
                child: Text(textContent,
                    textAlign: TextAlign.center,
                    style: const TextStyle(
                        fontSize: 16,
                        color: HDColors.black,
                        fontWeight: FontWeight.w600)))),
        contentPadding: const EdgeInsets.all(0),
        actions: [
          SizedBox(
              width: 400,
              child: Row(
                children: [
                  Visibility(
                      visible: textCancel != '' && textCancel != null,
                      child: Expanded(
                          child: InkWell(
                              onTap: () {
                                Get.back();
                                if (onCancel != null) onCancel();
                              },
                              child: Container(
                                  padding: const EdgeInsets.only(
                                      top: 15, bottom: 15),
                                  decoration: const BoxDecoration(
                                    border: Border(
                                        top: BorderSide(
                                            color: HDColors.dividerColor,
                                            width: 0.8),
                                        right: BorderSide(
                                            color: HDColors.dividerColor,
                                            width: 0.4)),
                                  ),
                                  child: Center(
                                    child: Text(textCancel,
                                        style: const TextStyle(
                                            fontSize: 16,
                                            color: HDColors.black,
                                            fontWeight: FontWeight.w500)),
                                  ))))),
                  Visibility(
                      visible: (textConfirm != '' &&
                          textConfirm != null &&
                          onConfirm != null),
                      child: Expanded(
                          child: InkWell(
                              onTap: () {
                                Get.back();
                                if (onConfirm != null) onConfirm();
                              },
                              child: Container(
                                  padding: const EdgeInsets.only(
                                      top: 15, bottom: 15),
                                  decoration: const BoxDecoration(
                                    border: Border(
                                        top: BorderSide(
                                            color: HDColors.dividerColor,
                                            width: 0.8),
                                        left: BorderSide(
                                            color: HDColors.dividerColor,
                                            width: 0.4)),
                                  ),
                                  child: Center(
                                    child: Text(textConfirm,
                                        style: const TextStyle(
                                            fontSize: 16,
                                            color: HDColors.blue,
                                            fontWeight: FontWeight.w500)),
                                  )))))
                ],
              ))
        ]);
  }
}

///时间选择模式枚举
enum DateTimePickerMode {
  /// Display DatePicker
  date,

  /// Display TimePicker
  time,

  /// Display DateTimePicker
  datetime,
}
